/*
 *      UnarySub.java
 *
 *      Copyright 2008 Pablo Nicolas Diaz Bilotto <pablonicolas.diaz@gmail.com>
 *
 *      This program is free software; you can redistribute it and/or modify
 *      it under the terms of the GNU General Public License as published by
 *      the Free Software Foundation; either version 2 of the License, or
 *      (at your option) any later version.
 *
 *      This program is distributed in the hope that it will be useful,
 *      but WITHOUT ANY WARRANTY; without even the implied warranty of
 *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *      GNU General Public License for more details.
 *
 *      You should have received a copy of the GNU General Public License
 *      along with this program; if not, write to the Free Software
 *      Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 *      MA 02110-1301, USA.
 */
package ilanguage.iltokens.operations.arimethic;

import lexer.EntryType;
import i386CodeGenerator.RegisterAssigner;
import ilanguage.iltokens.IL_SSA;
import ilanguage.iltokens.operations.OneOperandIL_SSA;
import ilanguage.iltokens.operations.Operations;

/**
 * The Class UnarySub.
 */
public class UnarySub extends OneOperandIL_SSA {

	/**
	 * Instantiates a new unary sub.
	 * 
	 * @param token
	 *            the token
	 */
	public UnarySub(IL_SSA token) {
		super(token);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see ilanguage.iltokens.SequenciableToken#GenerateCodeMachine()
	 */
	public String GenerateCode() {
		String firstArg = this.getFirstArg().getResultRegister();
		String codigo = "";
		codigo += "\t; Comienzo de Resta unaria\n";

		if (this.solveTypes() == EntryType.INT)
			codigo += subINTGenerateCode("0", firstArg);
		else {
			codigo += "\t\tfldz\n";
			codigo += "\t\tfstp varauxDouble\n";
			codigo += subDOUBLEGenerateCode("varauxDouble", firstArg);
		}

		codigo += "\t; Fin de Resta unaria\n\n";
		return codigo;
	}

	

	private String subINTGenerateCode(String firstArg, String secondArg) {

		String codigo = "";
		if (RegisterAssigner.getInstance().esRegistro(firstArg)) {
			codigo += "\t\tsub " + firstArg + " , " + secondArg + "\n";
			RegisterAssigner.getInstance().liberarRegistro(secondArg);
			this.setResultRegister(firstArg);
		} else { // si ninguno de los dos es registro
			String regAux = RegisterAssigner.getInstance().getRegistro();
			codigo += "\t\tmov " + regAux + " , " + firstArg + "\n";
			codigo += "\t\tsub " + regAux + " , " + secondArg + "\n";
			this.setResultRegister(regAux);

			RegisterAssigner.getInstance().liberarRegistro(firstArg);
			RegisterAssigner.getInstance().liberarRegistro(secondArg);
		}
		codigo += verificarOverFlowINT();
		codigo += verificarUnderFlowINT();
		return codigo;
	}

	private String subDOUBLEGenerateCode(String first, String second) {
		String codigo = "";

		if ((first != "FPU") && (second != "FPU")) {
			codigo += "\t\tfld " + first + "\n";
			codigo += "\t\tfld " + second + "\n";
		} else if ((first == "FPU") && (second != "FPU")) {
			codigo += "\t\tfld " + second + "\n";
		} else if ((first != "FPU") && (second == "FPU")) {
			codigo += "\t\tfld " + first + "\n";
			codigo += "\t\tfxch\n";
		}

		codigo += "\t\tfsub\n";
		this.setResultRegister("FPU");
		codigo += verificarUnderFlowDOUBLE();
		codigo += verificarOverFlowDOUBLE();
		return codigo;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see ilanguage.iltokens.operations.OperationToken#getOperationName()
	 */
	public String getOperationName() {
		return Operations.UNARY_SUB.toString();
	}

	@Override
	public boolean isDiscartable() {
		return super.isDiscartable();
	}

}
